package a.autocode;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jplat.tools.string.StringUtil;

/**
 * 配置文件信息.
 * 该类型配置较多,应该优化成环境配置和接口配置.
 * @author zhangcq
 * @date Dec 9, 2016
 * @comment
 */
public class JBaseConfigInfo
{
	//枚举配置命令.
	public static String CMD_SRC_PATH = "SRC_PATH";
	public static String CMD_BEAN_PACKAGE = "BEAN_PACKAGE";			//bean包名.
	public static String CMD_BEAN_TEMP_PATH = "BEAN_TEMP_PATH";		//bean模板.
	public static String CMD_PORT_PATH = "PORT_PATH";				//接口描述路径.
	public static String CMD_PORT_BACKUP_PATH = "PORT_BACKUP_PATH";	//接口描述备份包.
	
	public static String CMD_BEAN_NAME = "BEAN_NAME";
	public static String CMD_BEAN_DESC = "BEAN_DESC";				//app的bean接口描述.
	public static String CMD_GDATE = "GDATE";						//生成时间.
	public static String CMD_CHANGE = "CHANGE";						//最新修改内容.
	public static String CMD_REFURL = "REFURL";						//引用的URL.
	
	public static String CMD_REQUEST = "REQUEST";
	public static String CMD_RESPONSE = "RESPONSE";
	public static String CMD_LIST_START = "LIST_START";
	public static String CMD_LIST_END = "LIST_END";
	public static String CMD_APPNAME = "APPNAME";					//系统描述.
	public static String CMD_END = "END";
	
	//解析行为.
	public int action = ACT_NOACTION;
	
	//行为枚举
	public static final int ACT_NOACTION = 0;
	public static final int ACT_PARSE_REQ = 1;
	public static final int ACT_PARSE_REQ_LIST = 2;
	public static final int ACT_PARSE_RSP = 3;
	public static final int ACT_PARSE_RSP_LIST = 4;
	
	//beanType 前缀
	public static final String BEAN_TYPE_REQ = "Req";
	public static final String BEAN_TYPE_RSP = "Rsp";
	
	//配置信息保存.
	private String srcPath;					//源码位置.
	private String beanPackage;				//接口bean的包名.
	private String beanTemplatePath;		//bean模板位置.
	private String portPath;				//bean接口描述位置.
	private String portBackupPath;			//bean接口描述备份目录.
	
	//二选一App接口.
	private String beanName;		//bean名字关键字，并非全名.
	private String beanDesc;		//bean描述。
	private String gdate;			//接口定义时间.
	private String change;			//接口最新变化.
	private String refUrl;			//对应引用的URL.
	
	private String appName;			//应用描述.
	
	//保存配置,方便进行动态 映射.
	public Map<String,String> confMap = new HashMap<String,String>();
	
//	protected List<? extends JBaseFieldInfo> reqList;		//请求体定义
//	protected List<? extends JBaseFieldInfo> rspList;		//响应体定义
	
	//此处不好用作泛型.(非协变)
	protected List<JBaseFieldInfo> reqList;		//请求体定义
	protected List<JBaseFieldInfo> rspList;		//响应体定义
	
	public JBaseConfigInfo()
	{
		reqList = new ArrayList<JBaseFieldInfo>();
		rspList = new ArrayList<JBaseFieldInfo>();
	}
	
	/**
	 * 保存配置.
	 * @author zhangcq
	 * @date Dec 12, 2016
	 * @comment 
	 * @param key
	 * @param value
	 */
	public void putConfValue( String key, String value )
	{
		confMap.put(key, value);
	}
	
	public String getConfValue( String key )
	{
		return (String)confMap.get(key);
	}
	
	/**
	 * 获取bean的名字.
	 * @author zhangcq
	 * @date Dec 12, 2016
	 * @comment 
	 * @param type
	 * @return
	 */
	public String getBeanTypeName( String type )
	{
		return StringUtil.bigFirstCase(beanName)
				+StringUtil.bigFirstCase(type)
				+"Model";
	}
	
	/**
	 * 获取bean的输出目录.
	 * @author zhangcq
	 * @date Dec 6, 2016
	 * @comment 
	 * @return
	 */
	public String getBeanDir()
	{
		return getSrcPath()+File.separator+beanPackage.replace(".", File.separator );
	}
	
	/**
	 * 根据行为增加字段信息.
	 * @author zhangcq
	 * @date Dec 12, 2016
	 * @comment 
	 * @param fd
	 * @return
	 */
	public boolean addField( JBaseFieldInfo fd )
	{
		switch( action )
		{
		case JBaseConfigInfo.ACT_NOACTION:
			System.out.println("not prepared yet.");
			return false;
		case JBaseConfigInfo.ACT_PARSE_REQ:
			reqList.add(fd);
			break;
		case JBaseConfigInfo.ACT_PARSE_RSP:
			rspList.add(fd);
			break;
		case JBaseConfigInfo.ACT_PARSE_REQ_LIST:
			reqList.get(reqList.size()-1).fieldList.add(fd);
			break;
		case JBaseConfigInfo.ACT_PARSE_RSP_LIST:
			rspList.get(rspList.size()-1).fieldList.add(fd);		//增加到最后一个节点.
			break;
		default:
			break;
		}
		
		return true;
	}
	
	/**** getter-setter *******/
	public String getBeanPackage() {
		return beanPackage;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public void setBeanPackage(String beanPackage) {
		this.beanPackage = beanPackage;
	}

	public String getSrcPath() {
		return srcPath;
	}
	
	public void setSrcPath(String srcPath) {
		this.srcPath = srcPath;
	}
	
	public int getAction() {
		return action;
	}

	public void setAction(int action) {
		this.action = action;
	}

	public String getBeanTemplatePath() {
		return beanTemplatePath;
	}

	public void setBeanTemplatePath(String beanTemplatePath) {
		this.beanTemplatePath = beanTemplatePath;
	}

	public String getPortPath() {
		return portPath;
	}

	public void setPortPath(String portPath) {
		this.portPath = portPath;
	}

	public String getPortBackupPath() {
		return portBackupPath;
	}

	public void setPortBackupPath(String portBackupPath) {
		this.portBackupPath = portBackupPath;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	
	public String getGdate() {
		return gdate;
	}

	public void setGdate(String gdate) {
		this.gdate = gdate;
	}

	public String getBeanDesc() {
		return beanDesc;
	}
	
	public String getChange() {
		return change;
	}

	public void setChange(String change) {
		this.change = change;
	}

	public String getRefUrl() {
		return refUrl;
	}

	public void setRefUrl(String refUrl) {
		this.refUrl = refUrl;
	}

	public void setBeanDesc(String beanDesc) {
		this.beanDesc = beanDesc;
	}

	public List<JBaseFieldInfo> getReqList() {
		return reqList;
	}

	public void setReqList(List<JBaseFieldInfo> reqList) {
		this.reqList = reqList;
	}

	public List<JBaseFieldInfo> getRspList() {
		return rspList;
	}

	public void setRspList(List<JBaseFieldInfo> rspList) {
		this.rspList = rspList;
	}
}
